package com.nativelibs4java.buswing;

import javax.swing.table.TableModel;
import org.jdesktop.swingx.treetable.TreeTableModel;

/**
 * A TableModel to MiningModel adapter that implements SwingX's TreeTableModel interface
 * @author ochafik
 */
public class DefaultMiningModel extends AbstractMiningModel implements MiningTreeTableModel {

	public DefaultMiningModel(TableModel model) {
		super(model);
	}

	@Override
	public int getHierarchicalColumn() {
        if (getVisibleColumns().isEmpty())
            return -1;
		return getValueColumn().getVisibleIndex();
	}

	@Override
	public Object getValueAt(Object object, int columnIndex) {
		if (object instanceof DefaultMiningNode) {
			DefaultMiningNode node = (DefaultMiningNode)object;
			if (node.row != null)
				object = node.row;
			else
				return columnIndex == getHierarchicalColumn() ? node : null;
		}
		if (object instanceof DefaultMiningRow) {
			return getValueAt(((DefaultMiningRow)object).getUnderlyingIndex(), columnIndex);
		}
		else
			return object;
	}

	@Override
	public boolean isCellEditable(Object node, int columnIndex) {
		if (node instanceof DefaultMiningRow) {
			return isCellEditable(((DefaultMiningRow)node).getUnderlyingIndex(), columnIndex);
		}
		return false;
	}

	@Override
	public void setValueAt(Object value, Object object, int columnIndex) {
		if (object instanceof DefaultMiningRow) {
			setValueAt(value, ((DefaultMiningRow)object).getUnderlyingIndex(), columnIndex);
		}
	}

}